<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <!-- <role-search @search="reload" /> -->
      <!-- 数据表格 -->
      <ele-pro-table ref="table"
                     :columns="columns"
                     :datasource="datasource"
                     :selection.sync="selection">
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button size="small"
                     type="primary"
                     icon="el-icon-plus"
                     class="ele-btn-icon"
                     @click="openEdit()">
            添加
          </el-button>
        </template>
        <template v-slot:discountCouponType="{ row }">
          <el-tag v-if="row.discountCouponType == 1"
                  type="success">满减</el-tag>
          <el-tag v-if="row.discountCouponType == 2"
                  type="warning">折扣</el-tag>
          <el-tag v-if="row.discountCouponType == 3"
                  type="primary">新用户</el-tag>
        </template>
        <template v-slot:cardRollName="{ row }">
          <div class="bgGay flex-column-start">
            <div style="color: #6e6e6e; font-weight: 700">{{
              row.cardRollName
            }}</div>
            <!-- <div>{{ row.periodValidityStart}}-{{  row.periodValidityEnd }}</div> -->
            <div>{{ timeFormat(row.periodValidityStart) }} ~
              {{ timeFormat(row.periodValidityEnd) }}</div>
          </div>
        </template>

        <!-- 状态列 -->
        <template v-slot:whetherToRecommend="{ row }">
          <el-switch :active-value="1"
                     :inactive-value="2"
                     v-model="row.whetherToRecommend"
                     @change="editStatus(row)" />
        </template>
        <template v-slot:discount_money="{ row }">
          <div>
            {{ row.discount_money }}
          </div>
        </template>
        <template v-slot:discountCouponNumber="{ row }">
          <div>
            {{ row.discountCouponNumber ? row.discountCouponNumber : '----' }}
          </div>
        </template>

        <template v-slot:cardRollStatus="{ row }">
          <el-tag v-if="row.cardRollStatus == 1"
                  type="warning">未过期</el-tag>
          <el-tag v-if="row.cardRollStatus == 2"
                  type="success">未使用</el-tag>
          <el-tag v-if="row.cardRollStatus == 3"
                  type="danger">已过期</el-tag>
        </template>
        <!-- 优惠 -->
        <template v-slot:discounts="{ row }">
          <div>满{{ row.howMuchAvailable }}减{{ row.cardRollAmount }}</div>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link type="primary"
                   :underline="false"
                   icon="el-icon-edit"
                   v-if="row.cardRollStatus!==3"
                   @click="openEdit(row)">
            修改
          </el-link>

          <el-popconfirm class="ele-action"
                         title="确定要删除此该条信息吗？"
                         @confirm="remove(row)">
            <template v-slot:reference>
              <el-link type="danger"
                       :underline="false"
                       icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <role-edit :data="current"
               :drawerEditBoolen.sync="drawerEditBoolen"
               @done="reload" />
    <!-- 权限分配弹窗 -->
  </div>
</template>

<script>
import moment from 'moment';
// import RoleSearch from './components/role-search';
import RoleEdit from './components/role-edit';
import { couponList } from './constant/constant';
// import { couponList,delCoupon ,updateStatus} from '@/api/coupon/list';
import {
  cardRollList,
  cardRollDelete,
  cardRollDetail,
  updateDiscountCouponType,
  cardRollBatchDelete,
  cardRollModify
} from '@/api/couponManagement/couponList/index';
export default {
  name: 'Recharge',
  components: {
    // RoleSearch,
    RoleEdit
  },
  data () {
    return {
      couponList,
      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'cardRollName',
          label: '卡券名称',
          sortable: 'custom',
          showOverflowTooltip: true,
          width: 210,
          slot: 'cardRollName'
        },
        {
          prop: 'cardRollStatus',
          label: '卡券状态',
          align: 'center',
          sortable: 'custom',
          showOverflowTooltip: true,
          slot: 'cardRollStatus',
          minWidth: 110
        },
        {
          prop: 'discounts',
          label: '优惠',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'discounts'
        },
        {
          prop: 'discountCouponType',
          label: '卡券类型',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true,
          slot: 'discountCouponType',
          minWidth: 110
        },
        {
          prop: 'whetherToRecommend',
          label: '是否推荐',
          align: 'center',
          sortable: 'custom',
          showOverflowTooltip: true,

          minWidth: 110,
          slot: 'whetherToRecommend'
        },
        {
          prop: 'cardRollAmount',
          label: '卡券金额',
          align: 'center',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 90
        },
        {
          prop: 'discountCouponNumber',
          label: '卡券数量',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 90,
          slot: 'discountCouponNumber'
        },

        {
          prop: 'createdAt',
          label: '领取时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue, 'yyyy-MM-dd HH:mm');
          }
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 230,
          align: 'center',
          resizable: false,
          slot: 'action',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      drawerEditBoolen: false,
      // 是否显示导入弹窗
      showAuth: false,
      showChoose: false
    };
  },
  computed: {
    // 是否开启响应式布局
    styleResponsive () {
      return this.$store.state.theme.styleResponsive;
    },

    // 实名认证
    timeFormat () {
      return function (time) {
        const originalDate = time;
        const formattedDate = moment(originalDate).format('YYYY-MM-DD');
        return formattedDate; // 输出：2023-10-19
      };
    }
  },
  activated () {
    this.reload();
  },
  methods: {
    /* 表格数据源 */
    async datasource ({ page, limit, where, order }) {
      //   const res = await updateDiscountCouponType()
      return cardRollList({
        page,
        limit,
        ...where,
        siteId: this.$store.state.site.info.id
      });
    },
    /* 刷新表格 */
    reload (where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 显示编辑 */
    async openEdit (row) {
      if (row?.id) {
        const res = await cardRollDetail({ id: row.id });
        console.log(res);
        this.current = res;
        // 用户余额管理
        this.drawerEditBoolen = !this.drawerEditBoolen;
      } else {
        this.drawerEditBoolen = !this.drawerEditBoolen;
        this.current = null;
      }
    },
    /* 更改状态 */
    editStatus (row) {
      const loading = this.$loading({ lock: true });
      cardRollModify({
        id: row.id,
        whetherToRecommend: row.whetherToRecommend,
        siteId: this.$store.state.site.info.id
      })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.status = !row.status ? 1 : 2;
          this.$message.error(e.message);
        });
    },
    /* 显示分配权限 */
    openAuth (row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove (row) {
      const loading = this.$loading({ lock: true });
      cardRollDelete(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch () {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      console.log(this.selection);
      this.$confirm('确定要删除选中的信息吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          console.log(this.selection.map((d) => d.id));
          cardRollBatchDelete(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    }
  }
};
</script>
<style lang="less" scoped>
.bgGay {
  background-color: #f1f1f1;
  //   width: 150px;
  height: 70px;
  padding: 5px;
  border-radius: 4px;
}
</style>
